import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { JwtHelperService } from '@auth0/angular-jwt';

@Injectable({
    providedIn: 'root'
})

export class AuthGuard implements CanActivate {
    constructor(
        private router: Router,
        public jwtHelper: JwtHelperService
    ) {

    }

    canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot
    ): any {
        if (this.jwtHelper.isTokenExpired()) {
            console.log(this.jwtHelper.isTokenExpired());
            this.router.navigateByUrl('/pages/auth/login').then();
        }

        return true;
    }
}